package com.hd123.rumba.action.qt;

import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import com.hd123.rumba.jsp.RTreeNode;
import com.hd123.rumba.qt.def.IQueryDef;
import com.hd123.rumba.qt.def.IQueryDefinitionMgr;
import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.runtime.NamingMgr;

public class NavDefinitions extends ActionAbstractImpl implements Action {

  private static final String TEMPLATE_QUERYMENU = "rumba/qt2/QueryMenuTreeView.jsp";

  public String execute() throws Exception {
    String startnode = (String) dd.getFormValue("start");
    String url = ActionConstants.NOTIMPLEMENTED;
    if (startnode == null)
      startnode = "listTreeView";
    if (startnode.equals("listTreeView")) {
      url = executeWithPermission("listTreeView", null);
    }
    return url;
  }

  public String listTreeView() throws Exception {
    RTreeNode root = createTreeNode();
    dd.putFormValue("menuData", root.childrenJavaScript(0));
    dd.setWorkingTemplate(TEMPLATE_QUERYMENU);
    return ActionConstants.APPLICATIONFRAME;
  }

  private RTreeNode createTreeNode() throws Exception {
    IQueryDefinitionMgr qdm = (IQueryDefinitionMgr) NamingMgr.getInstance().lookupMgr(
        IQueryDefinitionMgr.class);
    HashMap<String, RTreeNode> hm = new HashMap<String, RTreeNode>();
    RTreeNode me = new RTreeNode();
    me.setCaption("查询目录");
    me.setObject("/");
    me.setUuid("/");
    hm.put("/", me);

    List<IQueryDef> queryDefs = qdm.getQueryDefsByUser(dd.getCurrentDomain(), dd.getCurrentUser());
    for (int i = 0; i < queryDefs.size(); i++) {
      IQueryDef queryDef = queryDefs.get(i);
      // 添加目录
      String base = "/";
      StringTokenizer st = new StringTokenizer(queryDef.getLocation(), "/");
      while (st.hasMoreElements()) {
        String folder = st.nextToken();
        RTreeNode parent = hm.get(base);
        if ("/".equals(base))
          base = "/" + folder;
        else
          base = base + "/" + folder;
        RTreeNode p = hm.get(base);
        if (p == null) {
          p = new RTreeNode();
          p.setCaption(folder);
          p.setObject(base);
          p.setUuid(base);
          parent.getChildren().add(p);
          hm.put(base, p);
        }
      }
      // 添加叶子
      RTreeNode parent = hm.get(queryDef.getLocation());
      RTreeNode p = new RTreeNode();
      p.setCaption(queryDef.getName());
      p.setObject(queryDef.getLocation() + queryDef.getFilename());
      p.setUuid(queryDef.getUuid());
      p.setOnSelect("onSelect");
      parent.getChildren().add(p);
    }
    return me;

    // if (QTConstants.LIST_VERBOSE) {
    // try {
    // QueryResultDescription qrd =
    // QueryMgr.getInstance().openQuery(QTConstants.QDHOME, folder,
    // dd.getCurrentUser());
    // if (qrd != null && qrd.getStartTime() != null &&
    // qrd.getResultCount().longValue() >= 0) {
    // sb.append("<br><a
    // href=\"ViewQuery?start=view&returnH=resultsReturn&qrd_uuid="
    // + qrd.getUuid() + "\">上次结果</a>:");
    // sb.append(DateUtil.dateToText(qrd.getStartTime()));
    // sb.append(" 记录数:" + qrd.getResultCountString());
    // sb.append(" 用时:" + JspLib.format(new
    // Integer(qrd.getTimeUsed().intValue() / 1000))
    // + "秒");
    // sb.append("<br><a
    // href=\"ViewQuery?start=view&returnH=resultsReturn&runQueryAgain=true"
    // + "&qrd_uuid=" + qrd.getUuid() + "\">重新查询</a>:" +
    // qrd.getQueryCriteria().getInfo());
    // }
    // } catch (Error e) {
    // } catch (Exception ex) {
    // }
    // }
  }

}
